package com.dinzeer.sjapadder.sa;

import com.dinzeer.legendreliclib.lib.util.check.CheckEntity;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.entity.SwordRainEntity;
import com.dinzeer.sjapadder.entity.SwordRainEntityLightning;
import mods.flammpfeil.slashblade.SlashBlade;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;

import java.awt.*;

import static com.dinzeer.sjapadder.Config.countNightingalesSlash;

public class Nightingales {
    public static void slash(LivingEntity livingEntity){
        LivingEntity target = CheckEntity.getPointedLivingEntity(livingEntity, 50.0F);
        if (target == null) return;
        double baseY = target.position().y +5;

        // 圆周参数
        double radius = 5.0; // 半径5*i1格
        int count = countNightingalesSlash; // 总数20个
        double angleStep = 360.0 / count; // 每18度一个实体
        
        // 优化：将worldIn获取移到循环外部
        Level worldIn = livingEntity.level();
        // 新增：随机保底索引确保至少一个闪电实体
        int guaranteedIndex = livingEntity.getRandom().nextInt(count);
        int LightningIndex=90-SlashBladeUtil.getRefine(livingEntity.getMainHandItem());
        int extraBase=0;
        if (LightningIndex<0){
            extraBase=((LightningIndex*-1)-90)/2;
            LightningIndex=0;

        }



        for (int i = 0; i < count; i++) {
            SwordRainEntity ss;
            
            // 保底机制：当索引匹配时强制生成闪电实体
            if (i == guaranteedIndex) {
                ss = new SwordRainEntityLightning(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(SlashBladeUtil.getColorCode(livingEntity.getMainHandItem()));
                ss.setDamage(SlashBladeUtil.getBaseAttackModifier(livingEntity.getMainHandItem())*0.2+extraBase);
            } 
            // 其他位置保持10%的闪电概率
            else if (livingEntity.getRandom().nextInt(100) > LightningIndex) {
                ss = new SwordRainEntityLightning(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(SlashBladeUtil.getColorCode(livingEntity.getMainHandItem()));
                ss.setDamage(SlashBladeUtil.getBaseAttackModifier(livingEntity.getMainHandItem())*0.2);
            } else {
                ss = new SwordRainEntity(SlashBlade.RegistryEvents.HeavyRainSwords, worldIn);
                ss.setColor(Color.BLUE.getRGB());
            }

            ss.doFire();
            ss.setIsCritical(false);
            ss.setOwner(livingEntity);
            ss.setRoll(0);
            ss.setForward(true);
            // force riding
            ss.startRiding(livingEntity, true);

            // 计算圆周坐标
            double angle = Math.toRadians(angleStep * i);
            double xOffset = radius * Math.cos(angle);
            double zOffset = radius * Math.sin(angle);

            // 设置位置
            ss.setPos(
                    target.position().x + xOffset,
                    baseY, // 统一高度
                    target.position().z + zOffset
            );
            worldIn.addFreshEntity(ss);
            livingEntity.playSound(SoundEvents.CHORUS_FRUIT_TELEPORT, 0.2F, 1.45F);
        }
    }

}
